package com.cw.jeeyt.core.impl;

import java.io.File;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cw.jeeyt.common.enums.scaffold.EnumScaffoldModuleCode;
import com.cw.jeeyt.common.utils.FileUtils;
import com.cw.jeeyt.service.bo.system.SysScaffoldConfigBo;
import com.cw.jeeyt.core.intf.SysScaffoldCore;
import com.cw.lang.common.utils.SequenceTool;

import cn.hutool.core.io.FileUtil;

@Service
public class SysScaffoldCoreImpl implements SysScaffoldCore {
    /** 脚手架工程模板路径 */
    @Value("scaffold.template.path")
    private String scaffoldTemplatePath;
    /** 脚手架工程模板路径 */
    @Value("scaffold.output.path")
    private String scaffoldOutputPath;

    @Value("scaffold.template.project.name")
    private String scaffoldTemplateProjectName;
    @Value("scaffold.template.module.biz.name")
    private String scaffoldTemplateModuleBizName;
    @Value("scaffold.template.module.common.name")
    private String scaffoldTemplateModuleCommonName;
    @Value("scaffold.template.module.facade.name")
    private String scaffoldTemplateModuleFacadeName;
    @Value("scaffold.template.module.test.name")
    private String scaffoldTemplateModuleTestName;
    @Value("scaffold.template.module.web.name")
    private String scaffoldTemplateModuleWebName;

    @Override
    public void initStructure(SysScaffoldConfigBo sysScaffoldConfigBo) {
        String projectId = SequenceTool.nextId();
        scaffoldOutputPath = scaffoldOutputPath + projectId + "/";
        String newProjectName = sysScaffoldConfigBo.getPomArtifactId();
        File file = new File(scaffoldOutputPath);
        if (!file.exists()) {
            FileUtils.createDirectory(scaffoldOutputPath);
        }
        FileUtil.copy(new File(scaffoldTemplatePath), new File(scaffoldOutputPath), true);
        //工程名重命名
        new File(scaffoldOutputPath + scaffoldTemplateProjectName).
            renameTo(new File(scaffoldOutputPath + newProjectName));
        //biz模块重命名
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleBizName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/" + sysScaffoldConfigBo.getPomArtifactId() + "-"
                + EnumScaffoldModuleCode.BIZ.getCode()));
        //common模块重命名
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleCommonName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/" + sysScaffoldConfigBo.getPomArtifactId() + "-"
                + EnumScaffoldModuleCode.COMMON.getCode()));
        //facade模块重命名
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleFacadeName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/" + sysScaffoldConfigBo.getPomArtifactId() + "-"
                + EnumScaffoldModuleCode.FACADE.getCode()));
        //test模块重命名
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleTestName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/" + sysScaffoldConfigBo.getPomArtifactId() + "-"
                + EnumScaffoldModuleCode.TEST.getCode()));
        //web模块重命名
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleWebName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/" + sysScaffoldConfigBo.getPomArtifactId() + "-"
                + EnumScaffoldModuleCode.WEB.getCode()));

        //biz模块包路径重命名
        //common模块包路径重命名
        //test模块包路径重命名
        //web模块包路径重命名
        //facade模块包路径重命名
    }

    @Override
    public void initPom(SysScaffoldConfigBo sysScaffoldConfigBo) {

    }

    @Override
    public void initCode(SysScaffoldConfigBo sysScaffoldConfigBo) {

    }

    public static void main(String[] args) {
        String scaffoldTemplatePath = "D:/chenweiProject/scaffold/template/scaffold/";
        String scaffoldOutputPath = "D:/chenweiProject/scaffold/output/";
        String scaffoldTemplateProjectName = "scaffold";
        String scaffoldTemplateModuleBizName = "scaffold-biz";
        String scaffoldTemplateModuleCommonName = "scaffold-common";
        String scaffoldTemplateModuleFacadeName = "scaffold-facade";
        String scaffoldTemplateModuleTestName = "scaffold-test";
        String scaffoldTemplateModuleWebName = "scaffold-web";
        String newProjectName = "haoran";
        String projectId = SequenceTool.nextId();
        scaffoldOutputPath = scaffoldOutputPath + projectId + "/";
        File outputFile = new File(scaffoldOutputPath);
        FileUtil.copy(new File(scaffoldTemplatePath), outputFile, true);
        new File(scaffoldOutputPath + scaffoldTemplateProjectName).
            renameTo(new File(scaffoldOutputPath + newProjectName));
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleBizName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/haoran-biz"));
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleCommonName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/haoran-common"));
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleFacadeName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/haoran-facade"));
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleTestName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/haoran-test"));
        new File(scaffoldOutputPath + newProjectName + "/" + scaffoldTemplateModuleWebName).
            renameTo(new File(scaffoldOutputPath + newProjectName + "/haoran-web"));
    }
}
